package org.example.library_management_system.Controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.library_management_system.Model.*;
import org.example.library_management_system.Service.BookService;
import org.example.library_management_system.constant.Constants;
import org.example.library_management_system.enums.BookStatus;
import org.example.library_management_system.enums.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping(value = "/book",produces = "application/json")
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping("getListByPage")
    public Result<PageResult> selectBooks(PageRequest pageRequest, HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_NAME);
        log.info("查看数据：{}",userInfo);
        if(userInfo ==null|| userInfo.getUserName()==null){
            return Result.noLogin();
        }

        PageResult data = bookService.selectBookByPage(pageRequest);
        return Result.success(data);
    }

    @RequestMapping("add")
    public String addBook(BookInfo bookInfo){
        //判断参数合法性
        log.info("点击添加后{}",bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName()) ||
                !StringUtils.hasLength(bookInfo.getAuthor()) ||
                !StringUtils.hasLength(bookInfo.getPublish()) ||
                bookInfo.getPrice() == null || !(bookInfo.getPrice() instanceof Integer) ||
                bookInfo.getCount() == null || !(bookInfo.getCount() instanceof Integer) ||
                bookInfo.getStatus() == null) {
            return "输入不合法";
        }

        try{
            Integer n = bookService.insertBook(bookInfo);
            if(n>0){
                return "添加成功";
            }
        } catch (Exception e) {
            log.error("数据有误，e",e);
//            e.getMessage();
        }
        return "添加失败";
    }


    @RequestMapping("selectBookInfoByIdOrName")
    public BookInfo selectBookInfoByIdOrName(BookInfo bookInfo){
        return bookService.selectBookInfoByIdOrName(bookInfo);
    }

    @RequestMapping("selectBookInfoById")
    public BookInfo selectBookInfoById(Integer id){
        return bookService.selectBookInfoById(id);
    }

    @RequestMapping("updateBookInfoById")
    public String updateBookInfoById(BookInfo bookInfo){
        log.info("传入参数：{}",bookInfo);
        Integer n = bookService.updateBookInfoById(bookInfo);
        if(n>0){
            return "修改成功";
        }else {
            return "出错，请联系程序员";
        }
    }


    @RequestMapping("deleteBookInfoById")
    public String deleteBookInfoById(Integer id){
        Integer n = bookService.deleteBookInfoById(id);
        if(n>0){
            return "删除成功";
        }else {
            return "出错，请联系程序员";
        }
    }

    @RequestMapping("deleteBookInfoByIds")
    public String deleteBookInfoByIds(@RequestParam List<Integer> ids){
        log.info("ids的值：{}",ids);
        Integer n = bookService.deleteBookInfoByIds(ids);
        if(n>0){
            return "删除成功";
        }else {
            return "出错，请联系程序员";
        }
    }
}
